namespace CSharpPro._1020;

public class Student
{
    
    private string _studentId;   
    private string _name;        
    private int _age;            
    private Dictionary<string, int> _courseScores; 
    
    public string StudentId
    {
        get => _studentId;
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("学号不能为空！");
            _studentId = value;
        }
    }

    public string Name
    {
        get => _name;
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("姓名不能为空！");
            _name = value;
        }
    }

    public int Age
    {
        get => _age;
        set
        {
            if (value < 15 || value > 30)
                throw new ArgumentOutOfRangeException("年龄必须在15-30岁之间！");
            _age = value;
        }
    }
    
    public List<string> CourseNames => new List<string>(_courseScores.Keys);
    
    public Student()
    {
        _courseScores = new Dictionary<string, int>();
    }
    
    public Student(string studentId, string name, int age) : this()
    {
        StudentId = studentId;
        Name = name;
        Age = age;
    }
    
    public void AddOrUpdateScore(string courseName, int score)
    {
        if (string.IsNullOrWhiteSpace(courseName))
            throw new ArgumentException("课程名不能为空！");
        if (score < 0 || score > 100)
            throw new ArgumentOutOfRangeException("成绩必须在0-100分之间！");

        if (_courseScores.ContainsKey(courseName))
            _courseScores[courseName] = score; 
        else
            _courseScores.Add(courseName, score); 
    }
    
    public double CalculateAverageScore()
    {
        if (_courseScores.Count == 0)
            return 0;

        int totalScore = 0;
        foreach (var score in _courseScores.Values)
            totalScore += score;

        return Math.Round((double)totalScore / _courseScores.Count, 1);
    }
    
    public int GetCourseScore(string courseName)
    {
        return _courseScores.TryGetValue(courseName, out int score) ? score : -1;
    }
    
    public void ShowStudentInfo()
    {
        Console.WriteLine("================ 学生信息 ================");
        Console.WriteLine($"学号：{StudentId}");
        Console.WriteLine($"姓名：{Name}");
        Console.WriteLine($"年龄：{Age}");
        Console.WriteLine("------------------------------------------");
        Console.WriteLine("课程成绩：");
        
        if (_courseScores.Count == 0)
        {
            Console.WriteLine("暂无课程成绩");
        }
        else
        {
            foreach (var item in _courseScores)
            {
                Console.WriteLine($"- {item.Key}：{item.Value}分");
            }
            Console.WriteLine($"------------------------------------------");
            Console.WriteLine($"所有课程平均分：{CalculateAverageScore()}分");
        }
        Console.WriteLine("===========================================\n");
    }
}
